﻿namespace MicroCloud.Options
{
    /// <summary>
    /// 从数据库选项
    /// </summary>
    public class SlaveDatabaseOption
    {
        #region 属性
        /// <summary>
        /// 获取或设置 从数据库选择策略
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "从数据库选择策略不能为空")]
        public string SlaveSelector { get; set; }
      
        /// <summary>
        /// 获取 从数据库选择策略枚举
        /// </summary>
        public SlaveDatabaseSelectorType SlaveSelectorEnum
        {
            get
            {
                SlaveDatabaseSelectorType selectorType = SlaveDatabaseSelectorType.Weight;
                var slaveSelectorLower = SlaveSelector?.ToLower();
                switch (slaveSelectorLower)
                {
                    case "weight":  //平滑权重
                        selectorType = SlaveDatabaseSelectorType.Weight;
                        break;
                    case "random":  //随机
                        selectorType = SlaveDatabaseSelectorType.Random;
                        break;
                    case "sequence":    //顺序轮询
                        selectorType = SlaveDatabaseSelectorType.Sequence;
                        break;
                }
                return selectorType;
            }
        }
  
        /// <summary>
        /// 获取或设置 从数据库集合
        /// </summary>
        public SlaveDatabaseInfo[] SlaveDatabases { get; set; }

        #endregion
    }

    /// <summary>
    /// 从数据库信息
    /// </summary>
    [DebuggerDisplay("{Name} - {Weight}")]
    public class SlaveDatabaseInfo : DataErrorInfoBase
    {
        #region "属性"
        /// <summary>
        /// 获取或设置 数据库名
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "从数据库名称不能为空")]
        public string Name { get; set; }

        /// <summary>
        /// 获取或设置 权重
        /// </summary>
        [Range(1, 100, ErrorMessage = "权重值范围为1-100")]
        public int Weight { get; set; } = 1;

        /// <summary>
        /// 获取或设置 数据库连接串
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "从数据库连接串不能为空")]
        public string ConnectionString { get; set; }
 
        /// <summary>获取 一条错误消息，指示此对象有什么问题。</summary>
        /// <returns>指示此对象存在什么问题的错误消息。默认值为空字符串（""）。</returns>
        public override string Error
        {
            get { return this["ConnectionString"]; }
        }

        #endregion

    }

}
